package com.example.ignite.cotroller;

import com.example.ignite.service.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
@RequestMapping("/item")
public class ItemIgniteController {

    @Autowired
    Cache<Integer,String> itemCache;

    @RequestMapping("/igniteput")
    public String put(){
        Integer key = new Random().nextInt();
        String value = key.toString();
        itemCache.put(key,value);
        return null;
    }

    @RequestMapping("/igniteget")
    public String get(){
        Integer key = new Random().nextInt();
        return itemCache.get(key);
    }

    @RequestMapping("/igniteputs")
    public String igniteputs(Integer startI, Integer endI) throws InterruptedException {
        System.out.println(System.currentTimeMillis() + " IgniteCluster1 start OK.");
        System.out.println(System.currentTimeMillis() + itemCache.get(1));
        for(int i=startI;i<endI;i++) {
            itemCache.put(i, " ignite" + i);
            System.out.println(System.currentTimeMillis() + " IgniteCluster " + i);
            Thread.sleep(1000);
        }
        return null;
    }

    @RequestMapping("/ignitegets")
    public String ignitegets(Integer startI, Integer endI){
        for(int i=startI;i<endI;i++) {
            System.out.println(System.currentTimeMillis() + " IgniteCluster " + itemCache.get(i));
        }
        return null;
    }
}
